<template>
  <div class="mg-t-30">
    <checked-title title="部件类案件排名">
      <template #right>
        <div
          :class="active == item.id ? 'active' : ''"
          :key="index"
          @click="tabChange(item.id, item.name)"
          class="title2 pointer"
          v-for="(item, index) in tabList"
        >{{ item.name }}</div>
      </template>
    </checked-title>
    <div>
      <div
        id="yangcheng-dianweipaiming"
        ref="yangchengchart"
        style="height: 400px"
      ></div>
    </div>
  </div>
</template>

<script>
import { BuJianMapDB } from '@/api'
import Bus from '@/untils/bus'
import { nextTick } from 'vue'

let chartDianWeiPaiMing = null

export default {
  props: {
    tab: {
      value: Object,
      default: true,
    }
  },
  data() {
    return {
      active: 'a34004',
      tabList: [],

      sortActive: 'desc', // asc
      quantity: [],
      numberTimes: [],
      categories: [],
      timer: null,
    }
  },
  mounted() {
    this.tabList = this.tab;
    if (this.tab.length) {
      this.bujianName = this.tab[0].name
      this.active = this.tab[0].id
    }
    this.$nextTick(() => {
      this.rankingList()
    })
  },
  beforeDestroy() {
    this.desHander()
  },
  methods: {
    // 案件数据
    rankingList(type) {
      const params = {
        name: this.bujianName,
      }
      BuJianMapDB.getMenuOrderNum(params).then(res => {
        let data = res.data.splice(0, 10)
        data.forEach((item, index) => {
          if (index >= 9) {
            return
          }
          this.categories.unshift(item.name)
          this.quantity.unshift(item.num)
          if (type == 'tabChange') {
            var chart = echarts.getInstanceByDom(document.getElementById('yangcheng-dianweipaiming'))
            var option = chart.getOption()
            option.series[0].data = this.quantity
            option.series[1].data = this.quantity
            chart.setOption(option)
            this.initChart()
          } else {
            this.initChart()
          }
        })
      })
    },

    // 更新
    tabChange(active, name) {
      this.active = active
      this.categories = []
      this.quantity = []
      this.bujianName = name
      this.rankingList('tabChange')
    },

    // 图表初始化
    initChart() {
      chartDianWeiPaiMing = echarts.init(document.getElementById('yangcheng-dianweipaiming'))
      var option = {
        grid: {
          left: '3%',
          right: '4%',
          bottom: '0%',
          top: '10%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(31, 73, 135, 0.7)',
          formatter: params => {
            let str = `${params[0].axisValue} ${params[0].data}件`
            return str
          }
        },
        xAxis: [
          { show: false },
          {
            type: 'value',
            name: '报警次数',
            position: 'right',
            alignTicks: true,
            offset: 80,
            axisLine: {
              show: true,
              lineStyle: {
                color: 'red'
              }
            },
            splitLine:{
              show: false
            },
            axisLabel: {
              formatter: '{value} ml'
            }
          }
        ],
        yAxis: [
          {
            name: '数值',
            type: 'category',
            // 刻度线
            axisTick: {
              show: false
            },
            // 坐标线
            axisLine: {
              show: false,
              lineStyle: {
                color: '#9ED5FF'
              }
            },
            data: this.categories,
            axisLabel: {
              // rotate: 50,
              margin: 15,
              formatter: function (value) {
                let ret = ''
                let max = 10
                let val = value.length
                let rowN = Math.ceil(val / max)
                if (rowN > 1) {
                  for (let i = 0; i < rowN; i++) {
                    let temp = ''
                    let start = i * max
                    let end = start + max
                    temp = value.substring(start, end) + '\n'
                    ret += temp
                  }

                  if (ret.length > 12) {
                    ret = ret.substring(0, 10) + '...'
                  }

                  return ret
                } else {
                  return value
                }
              }
            }
          }
        ],
        series: [
          {
            name: '数值',
            type: 'bar',
            zlevel: 3,
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(43, 136, 250, 0.1)'
            },
            barWidth: 15,
            itemStyle: {
              color: function (params) {
                let colorList = [
                  ['#42D9FF', '#18253B'],
                  ['#7CFF95', '#18253C'],
                  ['#4EA6FF', '#18253C'],
                  ['#FF6594', '#19253B'],
                  ['#4297FF', '#18253C'],
                  ['#42D9FF', '#18253B'],
                  ['#7CFF95', '#18253C'],
                  ['#4EA6FF', '#18253C'],
                  ['#FF6594', '#19253B'],
                  ['#4297FF', '#18253C'],
                  ['#42D9FF', '#18253B'],
                  ['#7CFF95', '#18253C'],
                  ['#4EA6FF', '#18253C'],
                  ['#FF6594', '#19253B'],
                  ['#4297FF', '#18253C'],
                  ['#42D9FF', '#18253B'],
                  ['#7CFF95', '#18253C'],
                  ['#4EA6FF', '#18253C'],
                  ['#FF6594', '#19253B'],
                  ['#4297FF', '#18253C'],
                ]
                let index = params.dataIndex
                if (params.dataIndex >= colorList.length) {
                  index = params.dataIndex - colorList.length
                }
                return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    offset: 0,
                    color: colorList[index][0]
                  },
                  {
                    offset: 1,
                    color: colorList[index][1]
                  }
                ])
              }
            },
            data: this.quantity
            // data: this.rankData.yData
          },
          {
            z: 1,
            type: 'pictorialBar',
            symbolPosition: 'end',
            symbolRotate: '180',
            symbolSize: [20, 15],
            itemStyle: {
              color: function (params) {
                let colorList = [['#42D9FF'], ['#7CFF95'], ['#4EA6FF'], ['#FF6594'], ['#4297FF']]
                let index = params.dataIndex
                if (params.dataIndex >= colorList.length) {
                  index = params.dataIndex - colorList.length
                }
                return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    offset: 0,
                    color: colorList[index]
                  },
                  {
                    offset: 1,
                    color: colorList[index]
                  }
                ])
              }
            },
            data: this.quantity,
            symbol: 'triangle',
            symbolOffset: ['52%', '0']
            // data: this.rankData.yData
          },
        ]
      }
      chartDianWeiPaiMing.setOption(option)
    },
    
    // 销毁实例
    desHander() {
      let myChart = echarts.init(this.$refs.yangchengchart)
      myChart.dispose()
    }
  }
}
</script>
<style lang="less">
.title_span {
  display: inline-block;
  width: 22px;
  height: 15px;
  img {
    display: block;
    height: 15px;
  }
}
#yangcheng-dianweipaiming {
  position: relative;
}

.posab {
  position: absolute;
  right: 20px;
  bottom: 385px;
  z-index: 2000;
  width: 20px;
  height: 20px;
}
.btns {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: block;
  padding: 0;
  background-color: transparent;
  border: 0px;
}
.el-icon-warning-outline {
  color: #fff;
  width: 20px;
  height: 20px;
  font-size: 20px;
}
</style>